<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Savesearch extends CI_Controller {


	var $_login_userid;
	
	function __construct()
	 {
		parent::__construct();
		$this->_login_userid=$this->get_login_userid();
	 }

	private function get_login_userid(){
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()){
			return $this->tank_auth->get_user_id();
		}else{
			return 0;
		}
	}
	
	public function insert(){
		if($this->_login_userid==0){
			$result_arr['status'] = 0;
			$result_arr['message'] = '你未登录或已经过期了，不能保存搜索记录！';
		}
		
		$this->load->database();
		$sql = "INSERT INTO save_search (words, in_date,uid) 
		VALUES (".$this->db->escape($_REQUEST['words']).",'".date("Y-m-d H:m:s")."','". $this->_login_userid."')";
		$this->db->query($sql);
		if($this->db->affected_rows()>0){
			$status=1;
		}else{
			$status=0;
		}
		$result_arr = array(
		   'status'=> $status,
		   'message'=>'消息'.$this->db->last_query(),
		   'data' => array(
				'newid'=>$this->db->insert_id()
		   )
		);
	
		$json_string = json_encode($result_arr);    
		echo $json_string; 		
	}

	
	public function del(){
		$id=$_POST['id'];
		$this->load->database();
//		$this->db->where('id',$id ); 
		$sql = "DELETE FROM save_search WHERE id=".$id." AND uid=".$this->_login_userid.";";
		$this->db->query($sql);
		if($this->db->affected_rows()>0){
			$status=1;
		}else{
			$status=0;
		}
		$result_arr = array(
		   'status'=> $status,
		   'message'=>'消息'.$this->db->last_query()
		);
	
		$json_string = json_encode($result_arr);    
		echo $json_string; 	
	
	}
	
	public function get(){
		$this->load->database();
		$this->db->where('uid', $this->_login_userid);
//		$this->db->order_by('sort','desc');
		$query = $this->db->get('save_search');
		if ($query->num_rows() > 0)
		{
			$rows_arr=array();
			foreach ($query->result_array() as $row)
			{
//				echo $row['id'];
				$rows_arr[$row['id']]=$row;
			}
//			print_r($rows_arr);
			$json_string = json_encode($rows_arr);    
			echo $json_string;  
		} 
	}
}
	
	